// 用于ASC声场分类的测试脚本
#include "AscDeepFileRT/Preprocess_CMP_RT.h"
#include <iostream>
#include "AscDeepFileRT/DeepCMP_RT.h"
//
#include "utool_AudioRT/Config.h"

int main(int argc, char **argv)
{
    // 先看看MEL谱的结果
    const char *model_file = argv[1];
    const char *audio_file = argv[2];

    // 1. 读取音频文件
    SF_INFO sfinfo = {0};
    SNDFILE* file = sf_open(audio_file, SFM_READ, &sfinfo);
    if (!file) {
        throw std::runtime_error("Failed to open audio file");
    }

    // 2. 读取音频数据
    std::vector<float> buffer(sfinfo.frames * sfinfo.channels);
    sf_read_float(file, buffer.data(), buffer.size());
    sf_close(file);

    // 3. 转换为单声道
    std::vector<float> mono(sfinfo.frames);
    if (sfinfo.channels > 1) {
        for (sf_count_t i = 0; i < sfinfo.frames; ++i) {
            float sum = 0.0f;
            for (int c = 0; c < sfinfo.channels; ++c) {
                sum += buffer[i * sfinfo.channels + c];
            }
            mono[i] = sum / sfinfo.channels;
        }
    } else {
        mono = std::move(buffer);
    }
    // 直接得到float数组送入模型看看结果

    // 初始化
    DeepCMP deepcmp;
    // 加载模型
    deepcmp.LoadModel("./weights/ASC_CPM.rknn");
    int Env_Class; // 声场类型0--干净语音,1--音乐,2--噪声,3--含噪语音
    // 运行模型
    std::vector<std::vector<float>> mel_filter_bank = create_mel_filter_bank(SAMPLE_RATE, WINDOW_SIZE, NUM_MELS);
    std::vector<std::vector<float>> mel_spectrogram;
    deepcmp.Run(mono, mel_spectrogram,mel_filter_bank,Env_Class);
    std::cout << "测试结果" << std::endl; 
}
